package Obj;

import Main.GameBegin;
import Utils.GameUtils;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class HeroObj extends GameObj {
    // ✅ 新增：英雄的10张动画帧图片
    private static final int FRAME_COUNT = 10;
    private Image[] frames = new Image[FRAME_COUNT];
    private int currentFrame = 0;
    private long lastTime = 0;
    private final int frameDelay = 50; // 每帧间隔毫秒数
    public HeroObj() {
    }

    public HeroObj(Image img, int width, int height, int x, int y, double speed, GameBegin gameBegin) {
        super(img, width, height, x, y, speed, gameBegin);
        // 初始化10张帧图片（根据实际文件名修改）
        for (int i = 0; i < FRAME_COUNT; i++) {
            frames[i] = Toolkit.getDefaultToolkit().getImage("plane/ws0" + i + ".png");
            System.out.println("加载第 " + i + " 张图片: " + frames[i]);
        }
        //添加鼠标的移动事件
        this.gameBegin.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
            HeroObj.super.x = e.getX() - width / 2;
            HeroObj.super.y = e.getY() - height / 2;
            }
        });
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }

    @Override
    public void paintSelf(Graphics g) {
        // 更新动画帧
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastTime > frameDelay) {
            //这种不循环越界的方式值得学一下
            currentFrame = (currentFrame + 1) % FRAME_COUNT;
            lastTime = currentTime;
        }
//开始绘制一帧画面
//│
//├── 获取当前时间 currentTime
//│
//├── 判断：currentTime - lastTime 是否超过 frameDelay？
//│       └─ 否 → 不变
//│       └─ 是 → 执行以下操作：
//│           ├── currentFrame = (currentFrame + 1) % FRAME_COUNT
//│           └── lastTime = currentTime
//│
//└── 使用 frames[currentFrame] 绘制图像

        // 绘制当前帧
        if (frames[currentFrame] != null) {
            g.drawImage(frames[currentFrame], x, y, width, height, null);
        } else {
            // 如果图片未加载成功，回退到默认图片
            g.drawImage(img, x, y, width, height, null);
        }
    }


    public HeroObj(Image img, int width, int height, int x, int y, double speed) {
        super(img, width, height, x, y, speed);
    }

    @Override
    public void setGameBegin(GameBegin gameBegin) {
        super.setGameBegin(gameBegin);
    }

    @Override
    public GameBegin getGameBegin() {
        return super.getGameBegin();
    }
}
